package repository.impl;

import DB.DBConnection;
import entity.Book;
import entity.BookCase;
import repository.BookRepository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BookRepositoryImpl implements BookRepository {
    @Override
    public List<Book> findAll() throws SQLException, ClassNotFoundException {
        Connection connection= DBConnection.getDB();
        String sql="select  *from book,bookcase where bookcase.id=book.id";
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        preparedStatement=connection.prepareStatement(sql);
        resultSet=preparedStatement.executeQuery();
        List<Book> list=new ArrayList<>();
        while(resultSet.next())
        {
            BookCase bookCase=new BookCase(resultSet.getInt(9),resultSet.getString(10));
            Book book=new Book(resultSet.getInt(1),resultSet.getString(2),resultSet.getString(3),resultSet.getString(4),resultSet.getInt(5),resultSet.getDouble(6),bookCase);
            list.add(book);
        }
        return list;
    }
}
